package com.ay.selfservicemachine.http.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;

import java.util.HashMap;
import java.util.Map;

public class LoadingDialogUtils {

    private Map<String, LoadingDialog> dialogMap;

    private LoadingDialogUtils() {
        dialogMap = new HashMap<>();
    }

    private static class Holder {
        private static LoadingDialogUtils mInstance = new LoadingDialogUtils();
    }

    public static LoadingDialogUtils getInstance() {
        return Holder.mInstance;
    }

    public LoadingDialog buildDialog(Context context, int msg, String tag, boolean isCancelable) {
        loadDialogDismiss(tag);
        LoadingDialog dialog = new LoadingDialog.Builder(context)
                .msg(context.getString(msg))
                .color(Color.WHITE)
                .width(100)
                .height(100)
                .cancelable(isCancelable)
                .canceledOnTouchOutside(isCancelable)
                .build();
        dialogMap.put(tag, dialog);
        dialog.show();
        return dialog;
    }

    public LoadingDialog buildDialog(Context context, int msg, String tag, DialogInterface.OnCancelListener listener) {
        LoadingDialog dialog = new LoadingDialog.Builder(context)
                .msg(context.getString(msg))
                .color(Color.WHITE)
                .width(100)
                .height(100)
                .cancelable(true)
                .canceledOnTouchOutside(true)
                .cancelListener(listener)
                .build();
        dialogMap.put(tag, dialog);
        dialog.show();
        return dialog;
    }

    public LoadingDialog buildDialog(Context context, String msg, String tag, DialogInterface.OnCancelListener listener) {
        LoadingDialog dialog = new LoadingDialog.Builder(context)
                .msg(msg)
                .color(Color.WHITE)
                .width(100)
                .height(100)
                .cancelable(true)
                .canceledOnTouchOutside(true)
                .cancelListener(listener)
                .build();
        dialogMap.put(tag, dialog);
        dialog.show();
        return dialog;
    }

    public LoadingDialog buildDialog(Context context, String s, String tag, boolean isCancelable) {
        LoadingDialog dialog = new LoadingDialog.Builder(context)
                .msg(s)
                .width(200)
                .height(60)
                .cancelable(isCancelable)
                .canceledOnTouchOutside(isCancelable)
                .download(true)
                .msgColor(Color.BLACK)
                .build();
        dialogMap.put(tag, dialog);
        dialog.show();
        return dialog;
    }

    public void loadDialogDismiss(String tag) {
        if (dialogMap.containsKey(tag)) {
            dialogMap.get(tag).dismiss();
            dialogMap.remove(tag);
        }
    }


}
